import axios from 'axios'
import {
  Toast
} from 'vant'
// import store from '@/store'
import router from '../router'
const service = axios.create({
  baseURL: process.env.NODE_ENV === 'development' ? '/api' : process.env.VUE_APP_API, // url = base api url + request url
  withCredentials: true, // send cookies when cross-domain requests
  headers: {
    'Content-Type': 'application/json',
  },
  timeout: 10000 // request timeout
})

let resUuid = ''
//请求拦截
service.interceptors.request.use(config => {
  Toast.loading({
    duration: 10000,
    message: '加载中...',
    forbidClick: true,
  });
  // store.commit('showLoading')
    const token = localStorage.eleToken
   if (config.url !== 'sys/login' && config.url !== 'sys/captcha' && config.url !== 'zwdd/login') {
     if (token) {
       config.headers['Authorization'] = token
     }
   } else {
     // 登录接口添加上uuid
     config.headers['uuid'] = resUuid
   }
   config.headers['affiliationApp'] = process.env.VUE_APP_AFFILIATION_APP
   config.headers['affiliationAppType'] = process.env.VUE_APP_AFFILIATION_APP_TYPE
   return config
}, error => {
  return Promise.reject(error)
})



//响应拦截
service.interceptors.response.use(response => {
  Toast.clear()
  // store.commit('hideLoading')
   const {
     headers: {
       authorization,
       'content-disposition': contentDisposition,
       uuid
     }
   } = response
   if (uuid) {
     console.log(uuid)
     resUuid = uuid
   }
  return response.data;

}, error => {
  Toast.clear()
  console.log(error.msg)
  // Message.error(error.response.data);
  // console.log(error)
  //获取错误状态码
  // const {
  //   status
  // } = error.response
  // if (status == 401) {
  //   Toast.fail('token失效，请重新登录')
  //   //清楚token
  //   localStorage.removeItem('eleToken')
  //   router.push('/login ')
  // }

  return Promise.reject(error)
})


export default service